<?php

namespace SLN0002\Service;

use Home\DAO\OrgDAO;
use Home\Service\BizlogService;
use Home\Service\PSIBaseExService;
use SLN0002\DAO\KaDAO;

/**
 * 记账 Service
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class KaService extends PSIBaseExService
{
  private $LOG_CATEGORY_GL = "总分类账记账";
  private $LOG_CATEGORY_DETAIL = "明细分类账记账";

  /**
   * 返回所有的公司列表
   *
   * @return array
   */
  public function companyList($fid)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params = [
      "loginUserId" => $this->getLoginUserId(),
      "fid" => $fid
    ];

    $dao = new OrgDAO($this->db());
    return $dao->getCompanyExList($params);
  }

  /**
   * 查询明细账簿列表
   */
  public function queryAccList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new KaDAO($this->db());

    return $dao->queryAccList($params);
  }

  /**
   * 登记明细账
   */
  public function keepDetail($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $db = $this->db();
    $db->startTrans();

    $dao = new KaDAO($db);
    $rc = $dao->keepDetail($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_DETAIL);

    $db->commit();

    return $this->ok();
  }

  /**
   * 查询明细账的账样列
   */
  public function queryFmtColsForDetailAcc($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new KaDAO($this->db());

    return $dao->queryFmtColsForDetailAcc($params);
  }

  /**
   * 查询明细账数据
   */
  public function queryDataForDetailAcc($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new KaDAO($this->db());
    return $dao->queryDataForDetailAcc($params);
  }

  /**
   * 查询一级科目列表
   */
  public function subjectList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new KaDAO($this->db());

    return $dao->subjectList($params);
  }

  /**
   * 查询总分类账数据
   */
  public function queryAccGlData($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new KaDAO($this->db());

    return $dao->queryAccGlData($params);
  }

  /**
   * 登记总账
   */
  public function keepGl($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $db = $this->db();
    $db->startTrans();

    $dao = new KaDAO($db);
    $rc = $dao->keepGl($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_GL);

    $db->commit();

    return $this->ok();
  }

  /**
   * 关闭明细账
   */
  public function closeAccDetail($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $db = $this->db();
    $db->startTrans();

    $dao = new KaDAO($db);
    $rc = $dao->closeAccDetail($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_DETAIL);

    $db->commit();

    return $this->ok();
  }

  /**
   * 取消关闭明细账
   */
  public function cancelCloseAccDetail($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $db = $this->db();
    $db->startTrans();

    $dao = new KaDAO($db);
    $rc = $dao->cancelCloseAccDetail($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_DETAIL);

    $db->commit();

    return $this->ok();
  }

  /**
   * 关闭总账
   */
  public function closeAccGL($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $db = $this->db();
    $db->startTrans();

    $dao = new KaDAO($db);
    $rc = $dao->closeAccGL($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_GL);

    $db->commit();

    return $this->ok();
  }

  /**
   * 取消关闭总账
   */
  public function cancelCloseAccGL($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $db = $this->db();
    $db->startTrans();

    $dao = new KaDAO($db);
    $rc = $dao->cancelCloseAccGL($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_GL);

    $db->commit();

    return $this->ok();
  }
}
